//
//  MainViewController.m
//  iShadowsocksR
//
//  Created by 付奎 on 2024/9/10.
//  Copyright © 2024 myGreateOrg. All rights reserved.
//

/**
 ssr初始化 + ["obfs": "tls1.2_ticket_auth", "host": "45.32.43.64", "ot_domain": "", "authscheme": "aes-256-cfb", "password": "password", "port": 14479, "protocol_param": "4757:123456:1726124281", "ota": false, "protocol": "auth_aes128_md5", "obfs_param": "", "ot_path": "", "ot_enable": false]
 */

#import "MainViewController.h"

#import "iShadowsocksR-Swift.h"
@import PotatsoModel;
@import PotatsoLibrary;
#import "NSDate+YCXTimeDescription.h"

@interface MainViewController ()

@property (strong, nonatomic) HomeVC *home; // <#注释#>
@property (strong, nonatomic) HomePresenter *homePerVC; // 注释

@property (weak, nonatomic) IBOutlet UILabel *ipL;
@property (weak, nonatomic) IBOutlet UIButton *connectBtn;
@property (weak, nonatomic) IBOutlet UILabel *statusL;


@end

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self setSSR];
    
}



#pragma mark -初始化SSR
- (void)setSSR{
    
    NSString *nowTime = [NSDate getNowTimeTimestamp];
    ProxyConfigurationViewController *pv = [[ProxyConfigurationViewController alloc] init];
    ProxyNode *upstreamProxyNode = [[ProxyNode alloc] init];
    upstreamProxyNode.typeRaw= @"SSR"; //  代理类型
    upstreamProxyNode.name= [NSString stringWithFormat:@"tem_%u", (arc4random() % 100000000)]; // 名称
    upstreamProxyNode.host= @"54.169.221.250"; // 服务器
    upstreamProxyNode.port= 8003; // 端口
    upstreamProxyNode.authscheme= @"aes-256-cfb" ; // "authscheme": "aes-256-cfb",
    upstreamProxyNode.password = @"password"; // 密码
    upstreamProxyNode.ssrProtocol= @"auth_aes128_md5"; // 协议插件
    upstreamProxyNode.ssrProtocolParam= [NSString stringWithFormat:@"%@:%@:%@", @"4757", @"123456", nowTime]; // @"1012:1111:0000000000"; // 协议插件参数
    upstreamProxyNode.ssrObfs= @"tls1.2_ticket_auth"; // 混淆插件
    upstreamProxyNode.ssrObfsParam= @""; // 混淆插件参数
    upstreamProxyNode.ssrotEnable= false; // SSRoT Enable
    upstreamProxyNode.ssrotDomain= @""; // SSRoT Domain
    upstreamProxyNode.ssrotPath= @""; // SSRoT Path
    
    [pv yyUpstreamProxyNodeWithUpstreamProxyNode:upstreamProxyNode];
    [pv yySave];
    
    HomePresenter *perVC = [[HomePresenter alloc] init];
    self.homePerVC = perVC;
    [perVC yyChooseProxyWithProxyNode:upstreamProxyNode];
    
    [self.home yyHandleRefreshUI];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleRefreshUI:) name:@"handleRefreshUINoti" object:nil]; // ssr通知
    
}



- (void)handleRefreshUI:(NSNotification *)noti{
    
    NSInteger status =  [noti.object intValue];
    switch (status) {
        case GVVPNStatusTypeOff: // VPN 未连接
            NSLog(@"ssr-未连接");
            self.statusL.text = @"未连接";
            self.connectBtn.selected = NO;
            [self.connectBtn setTitle:@"未连接" forState:UIControlStateNormal];
            break;
            
        case GVVPNStatusTypeConnected: // VPN 已连接上
            NSLog(@"ssr-已连接上");
            self.statusL.text = @"已连接上";
            self.connectBtn.selected = YES;
            break;
            
        case GVVPNStatusTypeConnecting: // VPN 连接中
            NSLog(@"ssr-连接中");
            self.statusL.text = @"连接中";
            self.connectBtn.selected = NO;
            [self.connectBtn setTitle:@"连接中" forState:UIControlStateNormal];
            break;

        case GVVPNStatusTypeDisconnecting: // VPN 断开连接中
            NSLog(@"ssr-断开连接中");
            self.statusL.text = @"断开连接中";
            self.connectBtn.selected = NO;
            [self.connectBtn setTitle:@"断开连接中" forState:UIControlStateNormal];
            break;
            
        default:
            break;
    }
}




- (IBAction)toConnect:(UIButton *)sender {
    
    if (!sender.selected) {
        [self setSSR];
    }
    
    [self.home handleConnectButtonPressed];
}


- (HomeVC *)home{
    
    if (!_home) {
        _home = [HomeVC new];
    }
    return _home;
}

@end
